/*
 ============================================================================
 Name		: LoadImageList.cpp
 Author	  : Leo
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CLoadImageList implementation
 ============================================================================
 */

#include "LoadImageList.h"

CLoadImageList::CLoadImageList()
	{
	isDecoder = EFalse;
	iPathList = NULL;
	iBitmapList = NULL;
	iMaskList = NULL;
	iImageDecoder = NULL;
	}

CLoadImageList* CLoadImageList::NewLC()
	{
	CLoadImageList* self = new (ELeave) CLoadImageList();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

CLoadImageList* CLoadImageList::NewL()
	{
	CLoadImageList* self = CLoadImageList::NewLC();
	CleanupStack::Pop(); // self;
	return self;
	}

int CLoadImageList::Callback(CFbsBitmap* aBitmap, CFbsBitmap* aMask)
	{
		if(iBitmapList)
			iBitmapList->AppendL(aBitmap);
		if(iMaskList)
			iMaskList->AppendL(aMask);
		
		isDecoder = EFalse;
		if(iPathList->Count() > iBitmapList->Count())
			{
				isDecoder = ETrue;			
				TInt nIndex = iBitmapList->Count();
				if(nIndex > 90)
					{
						nIndex ++;
						nIndex --;
					}
				iImageDecoder->Decoder(iPathList->At(nIndex));
				
				return 1;
			}
		
		return 0;
	}

_LIT(KPngPath, "c:\\data\\zk\\00");
_LIT(KPngEx, ".png");
void CLoadImageList::ConstructL()
	{
	iPathList = new (ELeave)CArrayFixFlat<TFileName>(100);
	iBitmapList = new (ELeave)CArrayFixFlat<CFbsBitmap*>(100);
	iMaskList = new (ELeave)CArrayFixFlat<CFbsBitmap*>(100);
	iImageDecoder = CRoImageDecoder::NewL(*this);
	
	for(int i=1; i<93; i++)
		{
			TFileName name(KPngPath);
			if(i<10)
				name.AppendNum(0);
			
			name.AppendNum(i);
			name.Append(KPngEx);
			this->AppendPathName(name);
		}
	
	}

int  CLoadImageList::AppendPathName(TFileName& aName)
	{
		if(iPathList)
			{
				iPathList->AppendL(aName);		
				if(!isDecoder && iImageDecoder)
					{
						iImageDecoder->Decoder(aName);	
						isDecoder = ETrue;
					}
				return iPathList->Count() -1;
			}
		return -1;
	}

CFbsBitmap* CLoadImageList::GetBitmap(int id)
	{
		if(iBitmapList && id>=0 && id < iBitmapList->Count())
			return iBitmapList->At(id);			
		return NULL;
	}

CFbsBitmap* CLoadImageList::GetMask(int id)
	{
	if(iMaskList && id>=0 && id < iMaskList->Count())
				return iMaskList->At(id);			
		return NULL;
	}

CLoadImageList::~CLoadImageList()
	{
	while(iMaskList && iMaskList->Count())
	{
	CFbsBitmap* p = iMaskList->At(0);
	delete p;
	iMaskList->Delete(0);			
	
	}
	if(iMaskList)
	{
	delete iMaskList;
	iMaskList = NULL;
	}
	
	while(iBitmapList && iBitmapList->Count())
	{
	CFbsBitmap* p = iBitmapList->At(0);
	delete p;
	iBitmapList->Delete(0);			
	
	}
	if(iBitmapList)
	{
	delete iBitmapList;
	iBitmapList = NULL;
	}
	
	delete iPathList;	
	delete iImageDecoder;
	}


